+function() {

var enterToNext = function(inputs, fn) {
	inputs.keydown(function(e) {
		var self = this;
		var me = $(this);
		if (e.keyCode == 13) {
			var index = inputs.index(this);
			if (index == inputs.length - 1) {
				fn && fn();
			} else {
				var input = null;
				inputs.each(function(i) {
					if (self == this) {
						input = inputs.get(i + 1);
					}
				})
				input && input.focus();
			}
			return false;
		}
	});
};
var formSubmit = function(form, fn) {
	var method = form.attr("method");
	var url = form.attr("action");
	var pp = $.getPageParams();
	var data = $.extend({}, pp, form.serializeJson());
	$.ajax({
		type : method,
		url : url,
		data : data,
		success : function(data) {
			var json = $.parseJSON(data);
			if (json.code == "SUCCESS") {
				fn && fn(json);
			} else {
				// 登录失败, 显示提示消息
				form.find(".message").text(json.message);
			}
		}
	});
};

var showRequiredPrompt = function() {
	var me = $(this);
	var form = me.closest("form");
	var placeholder = me.attr("placeholder");
	if (!placeholder) { // 通过label模拟实现的placeholder
		var id = me.attr("id");
		if (id) {
			var label = form.find("label[for=" + id + "]");
			placeholder = label.text();
		}
	}
	form.find(".message").text("请输入" + (placeholder || "必填项"));
};

// 登录
$(function() {
	var form = $("form[name=login]");
	form.find(".login-submit").click(function() {
		submit();
		return false;
	});

	var inputs = form.find("input[type=text],input[type=password]");

	var submit = function() {
		// 必填校验
		for (var i = 0; i < inputs.length; i++) {
			var me = inputs.eq(i);
			if (!me.val()) {
				showRequiredPrompt.call(me);
				me.focus();
				return false;
			}
		}
		// 提交
		formSubmit(form, function(data) {
			// 登录成功, 跳转至首页, backurl="/"有时候会跳到不存在的根路径, 改成跳到项目首页
			var backurl = data.body && data.body.backurl;
			if (!backurl || backurl == "/") { backurl = form.data("to"); }
			window.location.href = backurl;
		});
	};

	// 忘记密码
	form.find("a.repwd").click(function() {
		$("form[name=valid]").show().siblings().hide();
		$("form[name=valid]").find("[name=account]").focus();
	});

	// 回车键移到下一输入框, 最后一个输入提交
	enterToNext(inputs, submit);

	inputs.zchanged(function(n, o) {
		// 清空错误提示
		form.find(".message").text("");
		var me = $(this);
		var placeholder = me.attr("placeholder");
		if (!placeholder) { // 通过label模拟实现的placeholder
			var label = $([]);
			var id = me.attr("id");
			if (id) { label = form.find("label[for=" + id + "]"); }
			if (n == "") {
				label.show();
			} else {
				label.hide();
			}
		}
	});
});

// 忘记密码-验证手机
$(function() {
	var form = $("form[name=valid]");
	form.find(".login-submit").click(function() {
		submit();
		return false;
	});
	form.find(".login-cancel").click(function() {
		$("form[name=login]").show().siblings().hide();
		form.find(".login-send").removeAttr("disabled").countdown("clear");
		return false;
	});
	form.find(".login-send").click(function() {
		var $account = form.find("input[name=account]");
		if (!$account.val()) {
			showRequiredPrompt.call($account);
			$account.focus();
			return false;
		}
		var btn = $(this);
		btn.attr("disabled", "disabled");
		$.post(btn.data("url"), form.serialize(), function(data) {
			var json = typeof(data) == "string" ? $.parseJSON(data) : data;
			if (json.code == "SUCCESS") {
				btn.countdown(60, function() {
					btn.removeAttr("disabled");
				});
			} else {
				// 登录失败, 显示提示消息
				form.find(".message").text(json.message);
				btn.removeAttr("disabled");
			}
		});
		return false;
	});

	var inputs = form.find("input[type=text],input[type=password]");

	var submit = function() {
		// 必填校验
		for (var i = 0; i < inputs.length; i++) {
			var me = inputs.eq(i);
			if (!me.val()) {
				showRequiredPrompt.call(me);
				me.focus();
				return false;
			}
		}
		// 提交
		formSubmit(form, function(data) {
			// 重置密码
			$("form[name=repwd]").show().siblings().hide();
			$("form[name=repwd]").find("[name=password]").focus();
			form.find(".login-send").removeAttr("disabled").countdown("clear");
		});
	};

	// 回车键移到下一输入框, 最后一个输入提交
	enterToNext(inputs, submit);
});

// 忘记密码-重置密码
$(function() {
	var form = $("form[name=repwd]");
	form.find(".login-submit").click(function() {
		submit();
		return false;
	});
	form.find(".login-cancel").click(function() {
		$("form[name=login]").show().siblings().hide();
		return false;
	});

	var inputs = form.find("input[type=text],input[type=password]");

	var submit = function() {
		// 必填校验
		for (var i = 0; i < inputs.length; i++) {
			var me = inputs.eq(i);
			if (!me.val()) {
				showRequiredPrompt.call(me);
				me.focus();
				return false;
			}
		}
		if (form.find("[name=password]").val() != form.find("[name=newpassword]").val()) {
			form.find(".message").text("两次密码输入不一致");
			return false;
		}
		// 提交
		formSubmit(form, function(data) {
			// 找回密码成功
			$("form[name=login]").show().siblings().hide();
		});
	};

	// 回车键移到下一输入框, 最后一个输入提交
	enterToNext(inputs, submit);
});

}();



//定时检测实现zchanged事件
+function() {
	var TIMER = "CHANGE-TIMER";
	var VALUE = "LAST-VALUE";
	$.fn.zchanged = function(force, fn) {
		if ($.isFunction(force)) {
			fn = force; force = false;
		}
		return this.each(function() {
			var me = $(this);
			me.off(".zchanged")
			.on("focus.zchanged", function() {
				start.call(this, fn);
			})
			.on("blur.zchanged", function() {
				clear.call(this, fn);
			});
			if (me.attr("autofocus")) {
				start.call(this, fn);
			}
		});
	};
	var start = function(fn) {
		var self = this;
		var me = $(self);
		var timer = me.data(TIMER);
		if (timer) { window.clearInterval(timer); }
		timer = window.setInterval(function() {
			check.call(self, fn);
		}, 100);
		me.data(TIMER, timer);
	};
	var clear = function(fn) {
		check.call(this, fn);
		var me = $(this);
		var timer = me.data(TIMER);
		if (timer) { window.clearInterval(timer); me.removeData(TIMER); }
	};
	var check = function(fn) {
		var me = $(this);
		var o = me.data(VALUE) || "";
		var n = me.val();
		if (o != n) {
			me.data(VALUE, n);
			fn && fn.call(this, n, o);
		}
	};
}(jQuery);
